package cn.ee.dfs.esb.service.impl;

import cn.ee.dfs.common.entity.QueryRequest;
import cn.ee.dfs.common.utils.MD5Util;
import cn.ee.dfs.common.utils.RedisTool;
import cn.ee.dfs.common.webconst.SiteConstant;
import cn.ee.dfs.esb.entity.UserService;
import cn.ee.dfs.esb.mapper.UserServiceMapper;
import cn.ee.dfs.esb.service.IUserServiceService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 用户服务表 Service实现
 *
 * @author TWJ
 * @date 2019-08-14 15:34:25
 */
@Slf4j
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class UserServiceServiceImpl extends ServiceImpl<UserServiceMapper, UserService> implements IUserServiceService {

    @Autowired
    RedisTool redisTool;


    @Override
    public IPage<UserService> findUserServices(QueryRequest request, UserService userService) {

        Page<UserService> page = new Page<>(request.getPageNum(), request.getPageSize());
        return this.page(page, Wrappers.lambdaQuery(userService));
    }

    @Override
    public List<UserService> findUserServices(UserService userService) {

        return this.baseMapper.selectList(Wrappers.lambdaQuery(userService));
    }

    @Override
    @Transactional
    public void createUserService(UserService userService) {
        userService.setApplyTime(new Date());
        this.save(userService);
    }

    /**
     * 申请接口审核
     */
    @Override
    @Transactional
    public void updateUserServiceStatus(UserService userService) {
        userService.setUpdateTime(new Date());
        userService.setApplyTime(new Date());
        String key = MD5Util.encrypt16(userService.getApplyPerson(), userService.getUserServiceId() + userService.getSpName() + userService.getName() + userService.getVersion());
        userService.setAppKey(key);
        updateById(userService);
        if (userService.getApplyStatus() == UserService.APPLY_AGREE) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("ip", userService.getIps());
            map.put("frequency", userService.getFrequency());
            map.put("serviceId", "svc:" + userService.getServiceId());
            map.put("userServiceId", userService.getUserServiceId());
            map.put("serviceName", userService.getName());
            map.put("spName", userService.getSpName());
            redisTool.hmset(key, map);
        } else {
            redisTool.hdel(key, SiteConstant.usrSvcHMCache);
        }
    }

    /**
     *
     */
    @Override
    @Transactional
    public void updateUserService(UserService userService) {
        updateById(userService);
    }

    @Override
    @Transactional
    public void deleteUserService(String[] userServiceIds) {
        List<String> list = Arrays.asList(userServiceIds);
        list.forEach(id -> {
            String appKey = getById(id).getAppKey();
            if (!StringUtils.isEmpty(appKey)) redisTool.hdel(appKey,SiteConstant.usrSvcHMCache);
        });
        this.removeByIds(list);
    }

}
